<?php
require_once 'BaseController.php';

/**
 * 家賃相場検索クラス<br />
 *
 * @package    apamanshop
 * @author     SS笹川
 * @copyright  コピーライト(要確認)
 */
class Search_YachinsoubasearchController extends Search_BaseController
{
	public function indexAction()
	{
		$this->setMainTemplate('cms_search.tpl');
		/*
		 * 基本情報設定
		 */
		// 検索区分
		$this->setSearchKbn(Apm_Config_Search::SRHKBN_YACHINSOUBA, "yachinsoubasearch");
    	// 検索タイプ生成
		$this->_data['srhtype_jyusyo']   = Apm_Config_Search::SRHTYPE_JYUSYO;
		$this->_data['srhtype_enseneki'] = Apm_Config_Search::SRHTYPE_ENSENEKI;
		// 単一選択用フラグ
		$this->_data['SelFlag'] = 1;
		// 駅名選択なし
    	$this->_data['SelEkiNull'] = 1;

		//CSSの定義（デザイン）
		$this->addMetaCss('/css/search/search.css',array('type' => 'text/css','media' => 'all'));
		$this->addMetaCss('/css/shop/shop.css',array('type' => 'text/css','media' => 'all'));

		//JavaSriptの定義（アプリ）
		$this->addMetaJs('/app/js/search/Selensen.js');
		$this->addMetaJs('/app/js/search/Seltodohuken.js');
		$this->addMetaJs('/app/js/search/Yachinsouba.js');

		try{
			// 以下、検索画面によって条件分けする。
			// 家賃相場（市区町村）モデル 詳細
			if ($this->_params["Detail"] == Apm_Config_Search::SRHTYPE_JYUSYO ){

				$this->_data['nohead']   = $this->_isInline;
				$this->_data['chizuFlg'] = $this->_params['chizuFlg'];

				// 検索タイプ設定
				$this->_params['SrhType'] = Apm_Config_Search::SRHTYPE_JYUSYO;

				// CityCdを配列化
				if (!is_array($this->_params['CityCd'])) {
					$cityCd = $this->_params['CityCd'];
					$this->_params['CityCd'] = array();
					$this->_params['CityCd'][0] = $cityCd;
				}

				$this->_model_yachinsouba_sikucyoson = new Search_Model_Yachinsoubasikucyoson();

				// パラメータ設定
				if (empty($this->_params['type'])) {
					$this->_params['type'] = 1;
				}
				$this->_data['type'] = $this->_params['type'];
				if (empty($this->_params['graphcode'])) {
					$this->_params['graphcode'] = 1;
				}
				$this->_data['graphcode'] = $this->_params['graphcode'];
				if (empty($this->_params['madori'])) {
					$this->_params['madori'] = 1;
				}
				$this->_data['madori'] = $this->_params['madori'];
				
				$this->_data['SrhType']    = $this->_params['SrhType'];

				$this->_data['CityCd'] = $this->_params['CityCd'][0];

				$this->_params['PrefCd'] = array();
				$this->_params['PrefCd'][0] = substr($this->_params['CityCd'][0],0,2);

				// 家賃相場（市区町村）モデル
				$this->_model_yachinsouba_sikucyoson = new Search_Model_Yachinsoubasikucyoson();
				$result = array();
				$result = $this->_model_yachinsouba_sikucyoson->getYachinsoubaSikucyosonSyosaiData($this->_params);
				$result_graph = array();
				//$result_graph = $this->_model_yachinsouba_sikucyoson->getYachinsoubaSikucyosonGraphData($this->_params);
				
				// 表示用データに変換
				$this->_model_yachinsouba_sikucyoson->convertYachinsoubaSyosai($result,$result_graph,$this->_data);

				// データなしの場合
				if (empty($this->_data['PAR_NAME'])) {
					$result = array();
					$result = $this->_model_yachinsouba_sikucyoson->getSikucyosonData($this->_params);
					if (!empty($result[0])) {
						$this->_data['PAR_NAME'] = $result[0]['M01_TODOUFUKEN'].$result[0]['M01_SHIKUCHOUSON'];
					}
				}

				// 物件詳細の家賃相場テンプレート設定
				if((array_key_exists('rstdtlFlg', $this->_params) && $this->_params['rstdtlFlg'] == 1)
				|| (array_key_exists('chizuFlg',  $this->_params) && $this->_params['chizuFlg']  == 1)){
					$this->_main_helper = $this->_yachinsouba_for_rstdtl_helper;
				}else{
					$this->_main_helper = $this->_yachinsouba_sikucyoson_syosai_helper;
				}
			}

			// 都道府県検索画面の出力(初期呼び出し時)
			else if (! $this->_model_common->checkvalue($this->_params, "PrefCd"))
	    	{
				// H1のタイトル
				//$this->_data['h1_title'] = 'ご希望の都道府県を選択してください。';
				//$this->_main_helper = $this->_todohuken_radio_selsrh_helper;
				
				// メインテンプレート
				$this->_display_path = APPLICATION_PATH . '/modules/search/views/scripts/yachin.tpl';
				$this->addMetaCss('/search_share/yachin/css/add.css',array('type' => 'text/css','media' => 'all'));
				// 検索ボタン
				$this->_data['search_button_tpl'] = APPLICATION_PATH . '/modules/search/views/helpers/btn_yachin_pref.tpl';
				// メインブロック
				$this->_main_helper = $this->getApmHelper('seltodohuken_radio_renew.tpl');
				// リード文
				$this->_data['read_text'] = '賃貸マンション、アパート、借家・一戸建て毎に切り替えられる募集物件の相場情報をご用意しました。';
				// 左ペイン不要
				//$this->_noLeftPanel = true;
				// パンくずリセット
				$this->_breadcrumb->reset();
				$this->_breadcrumb->add(Apm_Config_Search::BREADCRUMB_TOP, $this->_baseurl);
				
				return;
			}
			else
			{
				//$this->_data['main_block']  = $this->_main_helper->getBlock($this->_data);
			}

			if($this->_isInline){
				// ヘッダ・フッター非表示テンプレート
				$this->_display_path = APPLICATION_PATH . '/modules/search/views/scripts/yachinsoubanohead.tpl';
			}else{
				// ノーマル
				$this->_display_path = APPLICATION_PATH . '/modules/search/views/scripts/search.tpl';
			}
		}catch(Exception $e){
			$this->_display_path = APPLICATION_PATH . '/modules/search/views/scripts/yachinsoubaerr.tpl';
		}
		// モバイルAPI
		if ($this->_params['isMobile'] == '1') {
			$this->mobileApiAct($this->_data);
		}
//print_r($this->_data);
	}

	public function cityAction()
	{
		if (empty($this->_params['PrefCd']) || empty($this->_params["SrhType"])) {
			Apm_Util::redirect($this->_baseurl."yachinsoubasearch/");
			exit();
		}
		
		// 検索区分
		$this->setSearchKbn(Apm_Config_Search::SRHKBN_YACHINSOUBA, "yachinsoubasearch/city/");
		
		$this->setMainTemplate('cms_search.tpl');
		// テンプレート
		$this->_display_path = APPLICATION_PATH . '/modules/search/views/scripts/yachin.tpl';
		$this->_data['read_text'] = "市区町村のアパマンショップ&nbsp;賃貸相場を間取り別にご覧いただけます。地域名をクリックすると、市区町村毎に家賃だけでなく、坪賃料や敷金、礼金などの相場もご覧いただけます。";
		//$this->_display_path = APPLICATION_PATH . '/modules/search/views/scripts/search.tpl';
		$this->_main_helper = $this->getApmHelper('yachinsouba_matrix.tpl');
		//$this->_main_helper = $this->_yachinsouba_sikucyoson_helper;
		
		//CSSの定義（デザイン）
		$this->addMetaCss('/css/search/search.css',array('type' => 'text/css','media' => 'all'));
		$this->addMetaCss('/css/shop/shop.css',array('type' => 'text/css','media' => 'all'));
		$this->addMetaCss('/search_share/yachin/css/add.css',array('type' => 'text/css','media' => 'all'));
		$this->addMetaCss('/search_share/css/index.css',array('type' => 'text/css','media' => 'all'));
		
		//JavaSriptの定義（アプリ）
		$this->addMetaJs('/js/common/common.js');
		$this->addMetaJs('/app/js/search/Yachinsouba.js');
		
		// パラメータ設定
		$this->_data['PrefCd']    = $this->_params['PrefCd'][0];
		$this->_data['PrefName']  = Apm_Config_Shop::$PREF_LIST[$this->_data['PrefCd']];
		$this->_data['SrhType']   = $this->_params['SrhType'];
		
		if (empty($this->_params['type'])) {
			$this->_params['type'] = 1;
		}
		$this->_data['type'] = $this->_params['type'];
		if (empty($this->_params['sortKey'])) {
			$this->_params['sortKey'] = 0;
		}
		$this->_data['sortKey'] = $this->_params['sortKey'];
		if (empty($this->_params['sortValue'])) {
			$this->_params['sortValue'] = 1;
		}
		$this->_data['sortValue'] = $this->_params['sortValue'];
		
		// 家賃相場（市区町村）モデル
		$this->_model_yachinsouba_sikucyoson = new Search_Model_Yachinsoubasikucyoson();
		$result = $this->_model_yachinsouba_sikucyoson->getYachinsoubaCityData($this->_params);
		// 表示用データに変換
		$this->_model_yachinsouba_sikucyoson->convertYachinsouba($result, $this->_params, $this->_data);
		
		// H1タイトル
		$this->_data['h1_title'] = $this->_data['PrefName'].'のアパマンショップ&nbsp;家賃相場';
		
		// モバイルAPI
		if ($this->_params['isMobile'] == '1') {
			$this->mobileApiAct($this->_data['yachinsouba']);
		}
	}

	public function ensenAction()
	{
		if (empty($this->_params['PrefCd']) || empty($this->_params["SrhType"])) {
			Apm_Util::redirect($this->_baseurl."search/yachinsoubasearch/");
			exit();
		}
		
		// 検索区分
		$this->setSearchKbn(Apm_Config_Search::SRHKBN_YACHINSOUBA, "yachinsoubasearch/ensen/");
		$this->_data['form_act'] = "yachinsoubasearch/eki/";
		// 単一選択用フラグ
		$this->_data['SelFlag'] = 1;
		// 駅名選択なし
    	$this->_data['SelEkiNull'] = 1;
		
		$this->setMainTemplate('cms_search.tpl');
		// テンプレート
		$this->_display_path = APPLICATION_PATH . '/modules/search/views/scripts/yachin.tpl';
		//$this->_display_path = APPLICATION_PATH . '/modules/search/views/scripts/search.tpl';
		
		//CSSの定義（デザイン）
		$this->addMetaCss('/css/search/search.css',array('type' => 'text/css','media' => 'all'));
		$this->addMetaCss('/css/shop/shop.css',array('type' => 'text/css','media' => 'all'));
		$this->addMetaCss('/search_share/yachin/css/add.css',array('type' => 'text/css','media' => 'all'));
		
		//JavaSriptの定義（アプリ）
		$this->addMetaJs('/app/js/search/Selensen.js');
		$this->addMetaJs('/app/js/search/Yachinsouba.js');
		
		// H1タイトル
		$this->_data['h1_title'] = 'ご希望の沿線を選択してください。';
		
		// リード文
		$this->_data['read_text'] = "沿線を選択し「家賃相場を見る」をクリックすると、アパマンショップ&nbsp;家賃相場の一覧表で確認できます。";
		
		// 沿線モデル
		$this->_model_ensen = new Search_Model_Selensen();
		
		$this->_data['selensen'] = array();
		$this->_data['selensen'] = $this->_model_ensen->getEnsenDataExceptBusSonota($this->_params);
		
		$this->_data['PrefCd'] = array();
		$this->_data['PrefCd'] = $this->_params['PrefCd'];
		
		// 表示用データに変換
		$this->_model_ensen->convertEnsenData($this->_data);
		
		// 検索ボタン
		$this->_data['search_button_tpl'] = APPLICATION_PATH . '/modules/search/views/helpers/btn_yachin_rail.tpl';
		
		$this->_main_helper = $this->getApmHelper('selensen_renew.tpl');
		//$this->_main_helper = $this->_ensen_helper;
	}

	public function ekiAction()
	{
		if (empty($this->_params['RailCd']) || empty($this->_params["SrhType"])) {
			Apm_Util::redirect($this->_baseurl."yachinsoubasearch/");
			exit();
		}
		
		// 検索区分
		$this->setSearchKbn(Apm_Config_Search::SRHKBN_YACHINSOUBA, "yachinsoubasearch/eki/");
		
		$this->setMainTemplate('cms_search.tpl');
		// テンプレート
		$this->_display_path = APPLICATION_PATH . '/modules/search/views/scripts/yachin.tpl';
		$this->_data['read_text'] = "駅のアパマンショップ&nbsp;賃貸相場を間取り別にご覧いただけます。駅名をクリックすると、駅毎に家賃だけでなく、坪賃料や敷金、礼金などの相場もご覧いただけます。";
		//$this->_display_path = APPLICATION_PATH . '/modules/search/views/scripts/search.tpl';
		$this->_main_helper = $this->getApmHelper('yachinsouba_matrix.tpl');
		//$this->_main_helper = $this->_yachinsouba_sikucyoson_helper;
		//$this->_main_helper = $this->_yachinsouba_ensen_helper;
		
		//CSSの定義（デザイン）
		$this->addMetaCss('/css/search/search.css',array('type' => 'text/css','media' => 'all'));
		$this->addMetaCss('/css/shop/shop.css',array('type' => 'text/css','media' => 'all'));
		$this->addMetaCss('/search_share/yachin/css/add.css',array('type' => 'text/css','media' => 'all'));
		$this->addMetaCss('/search_share/css/index.css',array('type' => 'text/css','media' => 'all'));
		
		//JavaSriptの定義（アプリ）
		$this->addMetaJs('/js/common/common.js');
		$this->addMetaJs('/app/js/search/Yachinsouba.js');
		
		// パラメータ設定
		$this->_data['PrefCd'] = $this->_params['PrefCd'][0];
		$this->_data['RailCd'] = $this->_params['RailCd'][0];
		//$this->_data['RailName']  = $railHash['RailCd'][$this->_params['PrefCd'][0].$this->_params['RailCd'][0]]['RAIL_NAME'];
		$this->_data['SrhType']    = $this->_params['SrhType'];
		
		if (empty($this->_params['type'])) {
			$this->_params['type'] = 1;
		}
		$this->_data['type'] = $this->_params['type'];
		if (empty($this->_params['sortKey'])) {
			$this->_params['sortKey'] = 0;
		}
		$this->_data['sortKey'] = $this->_params['sortKey'];
		if (empty($this->_params['sortValue'])) {
			$this->_params['sortValue'] = 1;
		}
		$this->_data['sortValue'] = $this->_params['sortValue'];
		
		// 家賃相場（沿線）モデル
		$this->_model_yachinsouba_ensen = new Search_Model_Yachinsoubaensen();
		$result = $this->_model_yachinsouba_ensen->getYachinsoubaEnsen($this->_params);
		
		// 表示用データに変換
		$this->_model_yachinsouba_ensen->convertYachinsouba($result, $this->_params, $this->_data);
		
		// データなしの場合
		if (empty($this->_data['PAR_NAME'])) {
			$model_ensen = new Search_Model_Selensen();
			$result = $model_ensen->getRailNameByRailCd($this->_params);
			if (!empty($result[0])) {
				$this->_data['PAR_NAME'] = $result[0]['M03_ENSEN_NAME'];
			}
		}
		
		// H1タイトル
		$this->_data['h1_title'] = $this->_data['PAR_NAME'].'のアパマンショップ&nbsp;家賃相場';
		
		// モバイルAPI
		if ($this->_params['isMobile'] == '1') {
			$this->mobileApiAct($this->_data['yachinsouba']);
		}
	}

	public function detailAction()
	{
		if (empty($this->_params['PrefCd']) || empty($this->_params["SrhType"])) {
			Apm_Util::redirect($this->_baseurl."search/yachinsoubasearch/");
			exit();
		}
		
		// 検索区分
		$this->setSearchKbn(Apm_Config_Search::SRHKBN_YACHINSOUBA, "yachinsoubasearch/detail/");
		
		$this->setMainTemplate('cms_search.tpl');
		// テンプレート
		$this->_display_path = APPLICATION_PATH . '/modules/search/views/scripts/yachin.tpl';
		//$this->_display_path = APPLICATION_PATH . '/modules/search/views/scripts/search.tpl';
		$this->_main_helper = $this->getApmHelper('yachinsouba_detail.tpl');
		//$this->_main_helper = $this->_yachinsouba_sikucyoson_syosai_helper;
		
		//CSSの定義（デザイン）
		$this->addMetaCss('/css/search/search.css',array('type' => 'text/css','media' => 'all'));
		$this->addMetaCss('/css/shop/shop.css',array('type' => 'text/css','media' => 'all'));
		$this->addMetaCss('/search_share/yachin/css/add.css',array('type' => 'text/css','media' => 'all'));
		$this->addMetaCss('/search_share/css/index.css',array('type' => 'text/css','media' => 'all'));
		
		//JavaSriptの定義（アプリ）
		$this->addMetaJs('/js/common/common.js');
		$this->addMetaJs('/app/js/search/Yachinsouba.js');
		
		// パラメータ設定
		if (empty($this->_params['type'])) {
			$this->_params['type'] = 1;
		}
		$this->_data['type'] = $this->_params['type'];
		if (empty($this->_params['graphcode'])) {
			$this->_params['graphcode'] = 1;
		}
		$this->_data['graphcode'] = $this->_params['graphcode'];
		if (empty($this->_params['madori'])) {
			$this->_params['madori'] = 1;
		}
		$this->_data['madori'] = $this->_params['madori'];
		
		$this->_data['SrhType']    = $this->_params['SrhType'];
		
		if ($this->_params["SrhType"] == Apm_Config_Search::SRHTYPE_JYUSYO ){
			// 市区町村詳細
			$this->_data['read_text'] = "市区町村のアパマンショップ&nbsp;賃貸相場を間取り別にご覧いただけます。";
			
			// CityCdを配列化
			if (!is_array($this->_params['CityCd'])) {
				$cityCd = $this->_params['CityCd'];
				$this->_params['CityCd'] = array();
				$this->_params['CityCd'][0] = $cityCd;
			}
			$this->_data['KeyCd'] = $this->_params['CityCd'][0];
			
			// 家賃相場（市区町村）モデル
			$this->_model_yachinsouba_sikucyoson = new Search_Model_Yachinsoubasikucyoson();
			$result = array();
			$result = $this->_model_yachinsouba_sikucyoson->getYachinsoubaSikucyosonSyosaiData($this->_params);
			$result_graph = array();
			//$result_graph = $this->_model_yachinsouba_sikucyoson->getYachinsoubaSikucyosonGraphData($this->_params);
			
			// 表示用データに変換
			$this->_model_yachinsouba_sikucyoson->convertYachinsoubaSyosai($result,$result_graph,$this->_data);
			
			// データなしの場合
			if (empty($this->_data['PAR_NAME'])) {
				$result = array();
				$result = $this->_model_yachinsouba_sikucyoson->getSikucyosonData($this->_params);
				if (!empty($result[0])) {
					$this->_data['PAR_NAME'] = $result[0]['M01_TODOUFUKEN'].$result[0]['M01_SHIKUCHOUSON'];
				}
			}
			
		} else {
			// 駅詳細
			$this->_data['read_text'] = "駅のアパマンショップ&nbsp;賃貸相場を間取り別にご覧いただけます。";
			
			// 家賃相場（沿線・詳細）モデル
			$this->_model_yachinsouba_ensen = new Search_Model_Yachinsoubaensen();
			
			// 路線図から来た場合、路線図CDから駅CDを取得
			if ($this->_model_common->checkvalue($this->_params, "r_eki_cd")) {
				$this->_model_yachinsouba_ensen->redirectFromRosenzu($this->_params);
			}
			
			// StCdを配列化
			if (!is_array($this->_params['StCd'])) {
				$cityCd = $this->_params['StCd'];
				$this->_params['StCd'] = array();
				$this->_params['StCd'][0] = $cityCd;
			}
			$this->_data['KeyCd'] = $this->_params['StCd'][0];
			$this->_data['PrefCd'] = $this->_params['PrefCd'][0];
			
			// 家賃相場（駅）検索
			$result = array();
			$result = $this->_model_yachinsouba_ensen->getYachinsoubaEnsenSyosaiData($this->_params);
			$result_graph = array();
			//$result_graph = $this->_model_yachinsouba_ensen->getYachinsoubaEnsenGraphData($this->_params);
			
			// 表示用データに変換
			$this->_model_yachinsouba_ensen->convertYachinsoubaSyosai($result,$result_graph,$this->_data);
			
			// データなしの場合
			if (empty($this->_data['PAR_NAME'])) {
				$result = array();
				$result = $this->_model_yachinsouba_ensen->getEkiData($this->_params);
				if (!empty($result[0])) {
					$this->_data['PAR_NAME'] = $result[0]['M03_ENSEN_NAME'].$result[0]['M03_EKI_NAME'];
				}
			}
			
		}
		
		// H1タイトル
		$this->_data['h1_title'] = $this->_data['PAR_NAME'].'のアパマンショップ&nbsp;家賃相場';
		
		// モバイルAPI
		if ($this->_params['isMobile'] == '1') {
			$this->mobileApiAct($this->_data['yachinsouba']);
		}
	}


}
?>